<?php
/**
 * Author: Silent Night
 * email: silentwolf_wp@163.com
 * Date: 2017/10/10
 * Time: 11:02
 */

namespace app\admin\model;

use think\Model;
use think;

class User extends Model
{


    public function getID($pid)
    {
        $data = array();
        $res = $this->where(['id' => $pid])
            ->find();
        if ($res) {
            $data = $res->toArray();
        }
        return $data;
    }

    public function lists($w, $pagesize = "10", $fields="*",$orderby="id desc")
    {
        $rdata = array();
        if (!empty($w))
            $this->where($w);

        $list = $this->field($fields)->order($orderby)->paginate($pagesize)->toArray();
//         echo $this->getLastSql();
        return $list;
    }

    public function getDataForWhere($w, $field = '*')
    {
        $data = array();
        $res = $this->where($w)->field($field)
            ->find();
        if ($res) {
            $data = $res->toArray();
        }

        return $data;
    }

    public function top10($w = array(), $fileds = array())
    {
        $data = array();
        $this->where($w);
        if (!empty($fileds)) {
            $this->field($fileds);
        }
        $this->order("score", "desc");
        $this->limit(10);
        $res = $this->select();
        if ($res) {
            $data = $res->toArray();
        }
        return $data;
    }

    public function getDataForWheres($w, $fileds = array())
    {
        $data = array();
        $this->where($w);
        if (!empty($fileds)) {
            $this->field($fileds);
        }
        $res = $this->select();
//          echo $this->getLastSql();
        if ($res) {
            $data = $res->toArray();
        }
        return $data;
    }


    public function getDataTops($w, $fileds = '*')
    {
        $data = array();
        $this->where($w);

        $this->field($fileds);
        $this->order('bsubject_score', 'desc');

        $res = $this->select();
        // echo $this->getLastSql();
        if ($res) {
            $data = $res->toArray();
        }
        return $data;
    }

    public function getContestTops($w, $fileds = '*')
    {
        $data = array();
        $this->where($w);

        $this->field($fileds);
        $this->order('contest_score', 'desc');

        $res = $this->select();
        // echo $this->getLastSql();
        if ($res) {
            $data = $res->toArray();
        }
        return $data;
    }


    public function getDataForCID($school_id, $cid)
    {
        $data = array();
        $res = $this->where(['cid' => $cid, 'school_id' => $school_id])
            ->find();
        if ($res) {
            $data = $res->toArray();
        }
        return $data;
    }

    public function getDataAll($school_id)
    {
        $data = array();
        $res = $this->where(['school_id' => $school_id])
            ->select();
        if ($res) {
            $data = $res->toArray();
        }
        return $data;
    }

    public function login($username, $pwd)
    {

        $data = array();
        $res = $this->where(['uname' => $username])
            ->find();
        if ($res) {
            $data = $res->toArray();
        }


        if (empty($data)) {
            return array('err' => 1, 'msg' => '登陆账号不存在');
        }
        if ($data['is_delete'] == 1) {
            return array('err' => 2, 'msg' => '该账号已被删除，请联系管理员');
        }
        if ($data['stat'] == 1) {
            return array('err' => 2, 'msg' => '该账号已被禁用，请找管理员解禁');
        }

        $pwd = UserPwdE($pwd);
        if ($data['pwd'] != $pwd) {
            return array('err' => 3, 'msg' => '登陆密码不正确');
        }
        return array('err' => 0, 'msg' => '登陆成功', 'data' => $data);
    }


    public function updateData($update, $id)
    {
        $res = $this->save($update, ['id' => $id]);
        return $res;
    }

    public function updateDataForWhere($update, $where)
    {
        $res = $this->save($update, $where);
//         echo $this->getLastSql();
        return $res;
    }

    public function loginLog($array, $id)
    {
        $res = $this->save($array, ['id' => $id]);
        return $res;
    }

    public function add($array)
    {
        $this->save($array);

        return $this->getLastInsID();
    }


    public function delData($w)
    {

        return $this->where($w)->delete();
    }

}